package com.wjjun.base.ui.base;

import android.app.Activity;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.luck.picture.lib.rxbus2.RxBus;
import com.qingmi.farmer.BuildConfig;
import com.qingmi.farmer.R;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.wjjun.basemvpvm.view.DataBindingFragment;
import com.wjjun.basemvpvm.viewmodel.BaseViewModel;

import dagger.android.support.AndroidSupportInjection;

/**
 * author Wjjun
 * Created 2018/3/6.
 */

public abstract class BaseFragment<VM extends BaseViewModel, VDB extends ViewDataBinding> extends DataBindingFragment<VM, VDB> implements BaseFragmentPresenter {

    @Override
    public void onAttach(Activity activity) {

        if (BuildConfig.DEBUG) {
            AndroidSupportInjection.inject(this);
        } else {
            try {
                AndroidSupportInjection.inject(this);
            } catch (Exception e) {
            }
        }
        super.onAttach(activity);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initContent();
        initData();
    }


    /**
     * 初始化控件
     */
    protected void initContent() {

    }

    /**
     * 初始化数据，发起网络请求等操作
     */
    protected void initData() {

    }

    @Override
    public void onToolBarLeftClick() {
        if (getActivity() != null) {
            getActivity().finish();
        }
    }

    @Override
    public void onToolBarTitleClick() {
    }

    @Override
    public void onToolBarFirstRightBtnClick() {
    }

    @Override
    public void onToolBarSecondRightBtnClick() {
    }

    @Override
    public void onToolBarThreeRightBtnClick() {
    }

    @Override
    public void onToolBarRightTxtClick() {
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public void onResume() {
        super.onResume();
        if (getChildFragmentManager().getFragments() != null && !getChildFragmentManager().getFragments().isEmpty()) {
            for (Fragment fragment : getChildFragmentManager().getFragments()) {
                fragment.onResume();
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        RxBus.getDefault().unregister(this);
    }

    public void showToast(int resId) {
        if (resId != 0) {
            ToastUtils.showShort(resId);
        }
    }

    public void showToast(String msg) {
        if (!TextUtils.isEmpty(msg)) {
            ToastUtils.showShort(msg);
        }
    }

    public void showLoadingDialog(int resId) {
        if (getActivity() == null) {
            showToast(R.string.error_activity_is_null);
            return;
        }
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showLoadingDialog(resId);
        }
    }

    public void showLoadingDialog(String msg) {
        if (getActivity() == null) {
            showToast(R.string.error_activity_is_null);
            return;
        }
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).showLoadingDialog(msg);
        }
    }


    public void dismissLoadingDialog() {
        if (getActivity() == null) {
            showToast(R.string.error_activity_is_null);
            return;
        }
        if (getActivity() instanceof BaseActivity) {
            ((BaseActivity) getActivity()).dismissLoadingDialog();
        }
    }

    public RxPermissions getRxPermissions() {
        if (getActivity() == null) {
            showToast(R.string.error_activity_is_null);
            return null;
        }
        if (getActivity() instanceof BaseActivity) {
            return ((BaseActivity) getActivity()).getRxPermissions();
        }
        return null;
    }
}
